﻿using System;
using System.Windows.Forms;
using Main.Structure;
using Main.Substructure;

namespace Main.Menus.Submenus
{
    public partial class AddAppointment : Form
    {
        /// <summary>
        /// Class constructor
        /// </summary>
        public AddAppointment()
        {
            InitializeComponent();
            Main();
        }

        /// <summary>This method executes code before the form is loaded.</summary>
        private void Main()
        {
            dateTimePicker.MinDate = DateTime.Today; // Set the minimum date chooser to today.
        }

        /// <summary>
        /// This method is executed when the user clicks on the "Cancel" button.
        /// This method disposes of the form.
        /// </summary>
        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        /// <summary>
        /// This method is executed when the user clicks on the "Add Appointment" button.
        /// This method creates a new appointment using the data entered, and adds it to the pool.
        /// </summary>
        private void btnAddAppointment_Click(object sender, EventArgs e)
        {
            var hour = numericHour.Value;
            // We gotta convert the time to military time and correct 12 o'clock times.
            #region Hour Check
            if (domainTOD.Text == "PM")
            {
                if (hour != 12)
                    hour += 12;
            }
            else
                if (hour == 12)
                    hour = 0;
            #endregion

            // Create a new DateTime variable uses the form data.
            DateTime DT = new DateTime(dateTimePicker.Value.Year, dateTimePicker.Value.Month, dateTimePicker.Value.Day,
                (int)hour, (int)numericMinute.Value, 0);
            // Create a new appointment by passing it that date's ticks and any notes the user may have entered.
            Appointment appt = new Appointment(DT.Ticks, txtBoxNotes.Text);

            if (!Database.Appointments.Add(appt))
                Utilities.Message("Appointment could not be added.", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
            else
                Utilities.Message("Appointment successfully added!", "Appointment Added", MessageBoxButtons.OK, MessageBoxIcon.Information);

            this.Dispose();
        }
    }
}
